
import json

from map_office.make_fragments import run_make_fragment
from map_office.register_fragments import run_register_fragment
from map_office.refine_registration import run_refine_fragment
from map_office.integrate_scene import run_integrate_scene
from map_office.realsense_recorder import record
import os

class cd:
    """Context manager for changing the current working directory"""
    def __init__(self, newPath):
        self.newPath = os.path.expanduser(newPath)

    def __enter__(self):
        self.savedPath = os.getcwd()
        os.chdir(self.newPath)

    def __exit__(self, etype, value, traceback):
        os.chdir(self.savedPath)
# conf="tutorial"

config={
        "name": "Captured frames using Realsense",
        "path_dataset": "dataset/realsense/",
        "path_intrinsic": "dataset/realsense/camera_intrinsic.json",
        "max_depth": 4.0,
        "voxel_size": 0.05,
        "max_depth_diff": 0.07,
        "preference_loop_closure_odometry": 0.1,
        "preference_loop_closure_registration": 10.0,
        "tsdf_cubic_size": 3.0,
        "icp_method": "color",
        "global_registration": "ransac",
        "python_multi_threading": True,
        "debug_mode": False,
    }
def set_default_value(config,key, value):
    if key not in config:
        config[key] = value
def initialize_config():
    # set default parameters if not specified
    set_default_value(config, "depth_map_type", "office")
    set_default_value(config, 'n_frames_per_fragment', 10)
    set_default_value(config, "n_keyframes_per_n_frame", 10)
    set_default_value(config, "min_depth", 0.3)
    set_default_value(config, "max_depth", 5.0)
    set_default_value(config, "voxel_size", 0.05)
    set_default_value(config, "max_depth_diff", 0.07)
    set_default_value(config, "preference_loop_closure_odometry", 0.1)
    set_default_value(config, "preference_loop_closure_registration", 5.0)
    set_default_value(config, "tsdf_cubic_size", 3.0)
    set_default_value(config, "icp_method", "color")
    set_default_value(config, "global_registration", "ransac")
    set_default_value(config, "python_multi_threading", "true")

    set_default_value(config, "folder_fragment", "fragments/")
    set_default_value(config, "template_fragment_posegraph",
                      "fragments/fragment_%03d.json")
    set_default_value(config, "template_fragment_posegraph_optimized",
                      "fragments/fragment_optimized_%03d.json")
    set_default_value(config, "template_fragment_pointcloud",
                      "fragments/fragment_%03d.ply")
    set_default_value(config, "folder_scene", "scene/")
    set_default_value(config, "template_global_posegraph",
                      "scene/global_registration.json")
    set_default_value(config, "template_global_posegraph_optimized",
                      "scene/global_registration_optimized.json")
    set_default_value(config, "template_refined_posegraph",
                      "scene/refined_registration.json")
    set_default_value(config, "template_refined_posegraph_optimized",
                      "scene/refined_registration_optimized.json")
    set_default_value(config, "template_global_mesh", "scene/integrated.ply")
    set_default_value(config, "template_global_traj", "scene/trajectory.log")

if __name__ == "__main__":
    path_output = './dataset/realsense/'
    record(path_output)
    initialize_config()
    run_make_fragment(config)
    run_register_fragment(config)
    run_refine_fragment(config)
    run_integrate_scene(config)
